package com.qk.manangement.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.common.response.PageResult;
import com.qk.entity.pojo.Activity;
import com.qk.manangement.service.ActivityService;
import com.qk.manangement.mapper.ActivityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService{

    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public PageResult<Activity> PageList(Integer channel, Integer type, Integer status, Integer page, Integer pageSize) {
        // 参数校验
        if (page == null || pageSize == null || page <= 0 || pageSize <= 0) {
            return new PageResult<>(0L, List.of());
        }

        // 设置分页参数
        PageHelper.startPage(page, pageSize);

        // 查询数据
        List<Activity> activityList = activityMapper.PageList(channel, type, status);

        // 构建分页结果
        PageInfo<Activity> pageInfo = new PageInfo<>(activityList);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public boolean delete(Integer id) {
        // 参数校验
        if (id == null) {
            return false;
        }

        // 执行删除操作并返回结果
        return activityMapper.delete(id) > 0;
    }


    @Override
    public boolean add(Activity activity) {
        // 参数校验
        if (activity == null) {
            return false;
        }

        // 校验必要字段
        if (activity.getName() == null ||
                activity.getDescription() == null ||
                activity.getType() == null ||
                activity.getChannel() == null ||
                activity.getStartTime() == null ||
                activity.getEndTime() == null) {
            return false;
        }

        // 设置创建和更新时间
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());

        // 插入活动信息
        return activityMapper.add(activity) > 0;
    }


    @Override
    public Activity getById(Integer id) {
        Activity activity = activityMapper.getById(id);
        if (activity != null) {
            return activity;
        }
        return null;
    }

    @Override
    public boolean update(Activity activity) {
        // 参数校验
        if (activity == null || activity.getId() == null) {
            return false;
        }

        // 校验必要字段
        if (activity.getName() == null ||
                activity.getDescription() == null ||
                activity.getType() == null ||
                activity.getChannel() == null ||
                activity.getStartTime() == null ||
                activity.getEndTime() == null) {
            return false;
        }

        // 验证活动是否存在
        Activity dbActivity = getById(activity.getId());
        if (dbActivity == null) {
            return false;
        }

        // 设置更新时间
        activity.setUpdateTime(LocalDateTime.now());

        // 根据活动类型清理无关字段
        if (activity.getType() != dbActivity.getType()) {
            if (activity.getType() == 1) {
                activity.setVoucher(null);
            } else if (activity.getType() == 2) {
                activity.setDiscount(null);
            }
        }

        // 执行更新操作
        return activityMapper.update(activity) > 0;
    }


    @Override
    public List<Activity> getListByType(Integer type) {

        return activityMapper.PageList(null, type,null);
    }
}




